package com.yt.base.controller.order;

import com.yt.base.controller.core.BaseController;
import com.yt.base.util.json.JacksonMapper;
import com.yt.core.MsgBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import com.yt.order.api.interfaces.InstitutionBranchService;
import com.yt.order.api.model.InstitutionBranch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/order/institutionBranch")
public class InstitutionBranchController extends BaseController {
    @Autowired
    private InstitutionBranchService institutionBranchService;

    @RequestMapping("/list")
    public
    @ResponseBody
    String list(HttpServletRequest request) {
        QueryFilter filter = new DynamicSQLFilterImpl();
        filter.addFilter(request.getParameterMap());
        List<InstitutionBranch> list = institutionBranchService.find(filter);
        int count = institutionBranchService.count(filter);
        JacksonMapper mapper = new JacksonMapper("yyyy-MM-dd HH:mm:ss");
        return mapper.toPageJson(list, count);
    }

    @RequestMapping("/multiDel")
    public
    @ResponseBody
    Object multiDel(@RequestParam(value = "ids", required = true) String ids) {
        MsgBean msg = new MsgBean();
        String[] idArr = ids.split(",");
        for (String id : idArr) {
            institutionBranchService.remove(new Long(id));
        }
        return msg.returnMsg();
    }

    @RequestMapping("/save")
    public
    @ResponseBody
    String save(InstitutionBranch institutionBranch) {
        InstitutionBranch branch = null;
        if (institutionBranch.getId() != null) {
            branch = institutionBranchService.get(institutionBranch.getId());
        } else {
            branch = new InstitutionBranch();
        }
        branch.setName(institutionBranch.getName());
        branch.setAddress(institutionBranch.getAddress());
        branch.setInstitutionId(institutionBranch.getInstitutionId());
        branch.setPhone(institutionBranch.getPhone());
        if (institutionBranch.getId() != null) {
            institutionBranchService.update(branch);
        } else {
            institutionBranchService.insert(branch);
        }

        return ("{success:true}");
    }

    @RequestMapping("/get")
    public
    @ResponseBody
    Object get(@RequestParam(value = "id", required = true) Long id) {
        MsgBean msg = new MsgBean();
        InstitutionBranch institutionBranch = institutionBranchService.get(id);
        msg.put("data", institutionBranch);
        msg.put("success", "true");

        return msg.returnMsg();
    }

    @RequestMapping("/getJSON")
    public String getJSON(HttpServletRequest request) {
        QueryFilter filter = new DynamicSQLFilterImpl();
        filter.addFilter(request.getParameterMap());
        filter.setPagingBean(null);
        List<InstitutionBranch> list = institutionBranchService.find(filter);

        StringBuffer buff = new StringBuffer("[ ");
        for (InstitutionBranch inst : list) {
            buff.append("['" + inst.getId() + "','" + inst.getName() + "'],");
        }
        buff.deleteCharAt(buff.length() - 1);
        buff.append("]");
        return buff.toString();
    }

}
